package com.mrd.gtimp.sup.service;

import com.mrd.base.BaseBiz;
import com.mrd.constant.SystemConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.FilterGroupOption;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.sup.entity.SupComConf;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */
@Service
public class SupComConfBiz extends BaseBiz<SupComConf> {


    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("supComConfDao")
    public void setBaseRepository(BaseRepository<SupComConf, String> baseRepository) {
        super.baseRepository = baseRepository;
    }


    public String ctCountsToString(int ctCounts, String com_id) throws QueryException {
        String resultString = ctCounts + "";
        String ctLevel = findCtLevel(com_id);
        if (!"".equals(ctLevel)) {
            resultString = ctCountsToStringByCtLevel(ctCounts, ctLevel);
        }
        return resultString;
    }


    public String ctCountsToStringByCtLevel(int ctCount, String ctLevels) {
        String resultString = ctCount + "";
        if (StringUtils.isNotEmpty(ctLevels)) {
            String[] rules = ctLevels.split(",");

            for (int i = 0; i < rules.length; i++) {
                String[] trules = rules[i].split(":");
                if (!trules[0].equals("0")) {
                    String[] crange = trules[0].split("-");
                    int upRange = 0;
                    int lowRange = 0;
                    if (crange.length == 1) {// 最大
                        upRange = Integer.parseInt(trules[0]);
                        if (ctCount >= upRange) {
                            resultString = trules[1];
                            break;
                        }
                    } else {
                        lowRange = Integer.parseInt(crange[0]);
                        upRange = Integer.parseInt(crange[1]);
                        if (ctCount >= lowRange && ctCount <= upRange) {
                            resultString = trules[1];
                            break;
                        }
                    }
                } else {
                    if (ctCount == 0) {
                        resultString = trules[1];
                        break;
                    }
                }
            }
        }
        return resultString;
    }


    public String findCtLevel(String com_id) throws QueryException {
        String ctLevel = "";
        Filter filters;
        filters = new Filter();
        filters.setGroupOp(FilterGroupOption.AND);
        SearchRule rule = new SearchRule("COM_ID", SearchRuleOption.eq, com_id);
        filters.addRule(rule);
        List<SupComConf> result = findEntityByFilter(1, 1, filters, null);
        if (result.size() == 1) {
            SupComConf supComConf = result.get(0);
            ctLevel = supComConf.getCT_LEVELS();
        }
        return ctLevel;
    }


    public String findVehicleLevel(String comId) throws QueryException {
        String veLevel = "";
        Filter filters;
        filters = new Filter();
        filters.setGroupOp(FilterGroupOption.AND);
        SearchRule rule = new SearchRule("COM_ID", SearchRuleOption.eq, comId);
        filters.addRule(rule);
        List<SupComConf> result = findEntityByFilter(1, 1, filters, null);
        if (result.size() == 1) {
            SupComConf supComConf = result.get(0);
            veLevel = supComConf.getVEHICLE_LEVELS();
        }
        return veLevel;
    }


    public int findTaxRange(String comId) throws Exception {
        int result = SystemConstants.TAX_ALERT_RANGE;

        Filter filters;
        filters = new Filter();
        filters.setGroupOp(FilterGroupOption.AND);
        SearchRule rule = new SearchRule("COM_ID", SearchRuleOption.eq, comId);
        filters.addRule(rule);
        List<SupComConf> resultObjList = findEntityByFilter(1, 1, filters, null);
        if (resultObjList.size() == 1) {
            SupComConf supComConf = resultObjList.get(0);
            result = supComConf.getTUISHUI_DAYS();
        }

        return result;
    }

}
